package com.sinog.biz.service.business.impl;

import com.sinog.biz.mapper.havexml.other.NoticeMapper;
import com.sinog.biz.service.business.NoticeService;
import com.sinog.core.model.NoticeVo;
import com.sinog.core.util.JsonUtil;
import com.sinog.core.utilbean.Constants;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @desc 小铃铛实现类
 * @author lzq
 * @date 2020-07-21 6:58 下午
 */
@Service
public class NoticeServiceImpl implements NoticeService {

    /**
     * 1:监狱发起核查 2:司法所完成核查 3:监狱发起衔接 4:司法所衔接中 5:司法局特殊核查流程核查 6:司法所/区县局申请解除帮教 7:核查失败司法所上报通知
     * 山西个性化功能:
     * 8:迁出地司法局审批消息 9:迁入地司法局审批消息 10:迁入地司法局审批拒绝 11:迁入地司法所接收，迁出地司法所通知 12:户籍迁入司法所接收通知
     */
    private static final String JY_HC = "1";
    private static final String SFS_DO_HC = "2";
    private static final String JY_XJ = "3";
    private static final String SFS_DO_XJ = "4";
    private static final String SFJ_DO_HC = "5";
    private static final String SFJ_DO_JC = "6";
    private static final String SFS_DO_SB = "7";
    private static final String QCD_SFJ_DO_SP = "8";
    private static final String QRD_SFJ_DO_SP = "9";
    private static final String QRD_SFJ_DO_SPJJ = "10";
    private static final String QRD_SFJ_DO_SP_TGJS = "11";
    private static final String QRD_SFJ_DO_SP_TGJS_SFSJS = "12";
    @Resource
    private NoticeMapper noticeMapper;

    /**
     * 查询各项待办事项的条数 -- 小铃铛
     * @param querykey querykey
     * @return Map<String,Integer>
     */
    @Override
    public Map<String,Integer> count(String querykey) {
        Map<String,Object> mapTem = JsonUtil.toMap(querykey);
        Map<String,Integer> map = new HashMap<>(16);
        String level = (String)mapTem.get("level");
        int remindAll = 0;
        if(Constants.OrgLevel.LEVEL_JY.equals(level) || Constants.OrgLevel.LEVEL_KSS.equals(level) || Constants.OrgLevel.LEVEL_SJZX.equals(level)) {
            //1、当前用户需要发起核查的条数(监狱)
            int hcNum = noticeMapper.selectCountHc(mapTem);
            map.put("remindHc",hcNum);
            remindAll += hcNum;
            //3、当前用户需要发起衔接的条数(监狱)
            int xjNum = noticeMapper.selectCountXj(mapTem);
            map.put("remindXj",xjNum);
            remindAll += xjNum;
        } else if(Constants.OrgLevel.LEVEL_SFS.equals(level)) {
            //2、当前用户需要完成核查的条数(司法所)
            int doHcNum = noticeMapper.selectCountDoHc(mapTem);
            map.put("remindDoHc",doHcNum);
            remindAll += doHcNum;
            //4、当前用户需要完成衔接的条数(司法所)
            int doXjNum = noticeMapper.selectCountDoXj(mapTem);
            map.put("remindDoXj",doXjNum);
            remindAll += doXjNum;
            int doJcNum = noticeMapper.selectCountDoJc(mapTem);
            map.put("remindDoJc",doJcNum);
            remindAll += doJcNum;
        } else if(Constants.OrgLevel.LEVEL_DSSFJ.equals(level) || Constants.OrgLevel.LEVEL_QXSFJ.equals(level) || Constants.OrgLevel.LEVEL_SFT.equals(level)) {
            //通知当前司法局/省厅上报的人数
            int doSbCount = noticeMapper.selectCountDoDb(mapTem);
            map.put("deathCount",doSbCount);
            remindAll += doSbCount;
            if (Constants.OrgLevel.LEVEL_QXSFJ.equals(level)){
                int doJcNum = noticeMapper.selectCountDoJc(mapTem);
                map.put("remindDoJc",doJcNum);
                remindAll += doJcNum;
            }
        }
        //5、工作提醒总数
        map.put("remindAll",99 < remindAll ? 100 : remindAll);
        return map;
    }

    /**
     * 查询待办事项
     * @param querykey querykey
     * @param type type
     * @return List<NoticeVo>
     */
    @Override
    public List<NoticeVo> list(String querykey,String type) {
        Map<String,Object> mapTem = JsonUtil.toMap(querykey);
        List<NoticeVo> noticeVos = new ArrayList<>(16);
        List<HashMap<String,Object>> list = new ArrayList<>(16);
        if(JY_HC.equals(type)) {
            list = noticeMapper.listHc(mapTem);
        } else if(SFS_DO_HC.equals(type)) {
            list = noticeMapper.listDoHc(mapTem);
        } else if(JY_XJ.equals(type)) {
            list = noticeMapper.listXj(mapTem);
        } else if(SFS_DO_XJ.equals(type)) {
            list = noticeMapper.listDoXj(mapTem);
        } else if(SFJ_DO_HC.equals(type)) {
            list = noticeMapper.listDoHcSfj(mapTem);
        } else if(SFS_DO_SB.equals(type)) {
            // 区县/地市代办，省厅待办
            list = noticeMapper.listDoDb(mapTem);
        } else if (SFJ_DO_JC.equals(type)) {
            list = noticeMapper.listDoJc(mapTem);
        } else if(QCD_SFJ_DO_SP.equals(type)){
            list = noticeMapper.listQcdSfjDoSp(mapTem);
        } else if(QRD_SFJ_DO_SP.equals(type)){
            list = noticeMapper.listQrdSfjDoSp(mapTem);
        } else if(QRD_SFJ_DO_SPJJ.equals(type)){
            list = noticeMapper.listQrdSfjDoSpJj(mapTem);
        } else if(QRD_SFJ_DO_SP_TGJS.equals(type)){
            list = noticeMapper.listQrdSfjDoSpTgjs(mapTem);
        } else if(QRD_SFJ_DO_SP_TGJS_SFSJS.equals(type)){
            list = noticeMapper.listQrdSfsDoSpTgjs(mapTem);
        }
        for(HashMap<String,Object> map : list) {
            NoticeVo noticeVo = new NoticeVo();
            noticeVo.setName((String)map.get("pname"));
            String addtime = (String)map.get("rjDate");
            String cjrq = (String)map.get("cjDate");
            String addTime = (String)map.get("addTime");
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            if (map.get("releaseDate") != null){
                String releaseDate = simpleDateFormat.format(map.get("releaseDate"));
                noticeVo.setReleaseDate(releaseDate);
            }
            String sfsname = (String)map.get("sfsname");
            noticeVo.setRjDate(addtime);
            noticeVo.setCjDate(cjrq);
            noticeVo.setAddTime(addTime);
            noticeVo.setType(type);
            noticeVo.setSfsname(sfsname);
            noticeVos.add(noticeVo);
        }
        return noticeVos;
    }
}